package cosplay.Services;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class CacheServices implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2796561694504329223L;
	@SuppressWarnings("unchecked")
	public static List<Integer> getFromCache(Cache cache, String sessionid, String filtername, int page)
	{
		if (!cache.isElementInMemory(sessionid))
		{
			return null;
		}
		else
		{
			if (!((Map<String,List<Integer>>)cache.get(sessionid).getValue()).containsKey(filtername+"resultcount"))
			{
				return null;
			}
			else
			{
				return ((Map<String,List<Integer>>)cache.get(sessionid).getValue()).get(filtername+"page"+page+"");
			}
		}
	}
	@SuppressWarnings("unchecked")
	public static int getCountFromCache(Cache cache, String sessionid, String filtername)
	{
		if (!cache.isElementInMemory(sessionid))
		{
			return 0;
		}
		else
		{
			if (!((Map<String,List<Integer>>)cache.get(sessionid).getValue()).containsKey(filtername+"resultcount"))
			{
				return 0;
			}
			else
			{
				return ((Map<String,List<Integer>>)cache.get(sessionid).getValue()).get(filtername+"resultcount").get(0);
			}
		}
	}
	@SuppressWarnings("unchecked")
	public static void removeFromCache(Cache cache, String filtername)
	{
		List<String> name =cache.getKeys();
		for (String sid :name)
		{
			Map<String,List<Integer>> cachemap = (Map<String, List<Integer>>) cache.get(sid).getValue();
			if (cachemap.containsKey(filtername+"resultcount"))
			{
				Set<String> results =cachemap.keySet();
				for(String resultname:results)
				{
					if (resultname.startsWith(filtername))
					{
						cachemap.remove(resultname);
					}
				}
			}
		}
	}
	@SuppressWarnings("unchecked")
	public static void insertToCache(Cache cache, String filtername, String sessionid, List<Integer> result,int pagesize)
	{
		result.addAll(result);
		result.addAll(result);
		result.addAll(result);
		result.addAll(result);
		result.addAll(result);
		result.addAll(result);
		result.addAll(result);
		if (cache.isElementInMemory(sessionid))
		{
			cosplay.Services.CacheServices.removeFromCache(cache, filtername);
		}
		else
		{
			Map<String,List<Integer>> res=new HashMap<String,List<Integer>>();
			cache.put(new Element(sessionid,res));
		}
		Map<String,List<Integer>> cachemap=(Map<String, List<Integer>>) cache.get(sessionid).getValue();
		List<Integer> resultcount=new ArrayList<Integer>();
		resultcount.add(Integer.valueOf(result.size()));
		cachemap.put(filtername+"resultcount", resultcount);
		if (resultcount.get(0)<pagesize)
		{
			cachemap.put(filtername+"page1", result);
		}
		else
		{
			int pagecount=(resultcount.get(0)%pagesize==0)?(resultcount.get(0)/pagesize):((resultcount.get(0)/pagesize)+1);
			List<Integer> temp=null;
			for (int i=1;i<=pagecount;i++)
			{
				temp=new ArrayList<Integer>();
				int start=(i-1)*pagesize;
				int end=((i*pagesize)>resultcount.get(0))?resultcount.get(0):(i*pagesize);
				//sublist from include to not include...
				//for (Integer d:result.subList((i-1)*pagesize, ((i*pagesize-1)>resultcount.get(0))?resultcount.get(0):(i*pagesize-1)))
				for (Integer d:result.subList(start, end))
					temp.add(d);
				//temp=result.subList((i-1)*pagesize, ((i*pagesize-1)>resultcount.get(0))?resultcount.get(0):(i*pagesize-1));
				cachemap.put(filtername+"page"+i+"",temp );
			}
		}
		cache.remove(sessionid);
		cache.put(new Element(sessionid,cachemap));
	}
}
